package com.mune.gulimall.order.web;

import com.mune.gulimall.order.service.OrderService;
import com.mune.gulimall.order.vo.OrderConfirmVo;
import com.mune.gulimall.order.vo.OrderSubmitVo;
import com.mune.gulimall.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {
  @Autowired
  OrderService orderService;

  @GetMapping("/toTrade")
  public String toTrade(Model model, HttpServletRequest request) throws ExecutionException, InterruptedException {
    OrderConfirmVo vo = orderService.confirmOrder();
    model.addAttribute("confirmData", vo);
    return "confirm";
  }

  @PostMapping("/submitOrder")
  public String submitOrder(OrderSubmitVo vo, Model model) {
    SubmitOrderResponseVo responseVo = orderService.submitOrder(vo);
    if (responseVo.getCode() == 0) {
      model.addAttribute("submitOrderResp", responseVo);
      return "pay";
    }
    return "redirect:http://order.gulimall.com/toTrade";
  }
}
